package cn.zyzpp.service.job.impl;

import cn.zyzpp.dao.NewJobDao;
import cn.zyzpp.entity.job.NewJob;
import cn.zyzpp.service.job.JobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

/**
 * Create by yster@foxmail.com 2018/5/22/022 16:16
 */
@Service
@CacheConfig(cacheNames = {"JobService"})
public class JobServiceImpl implements JobService {
    @Autowired
    private NewJobDao newJobDao;

    @Override
    @CacheEvict(allEntries = true)
    public void save(NewJob job, int uid) {
        job.setUid(uid);
        job.setJobDate(new Date());
        job.setOrUse(1);
        newJobDao.save(job);
    }

    @Override
    @Transactional
    @CacheEvict(allEntries = true)
    public void delect(int id) {
        newJobDao.deleteAllById(id);
    }

    @Override
    @Transactional
    @CacheEvict(allEntries = true)
    public void updata(NewJob job) {
        NewJob newJob = newJobDao.findAllById(job.getId());
        newJob.updata(job);
    }

    @Override
    @Cacheable(key = "targetClass + methodName +#p0")
    public List<NewJob> queryAll(int uid) {
        return newJobDao.findAllByUid(uid);
    }

    @Override
    @CacheEvict(allEntries = true)
    public NewJob query(int id) {
        return newJobDao.findAllById(id);
    }


    @Override
    @Cacheable(key = "targetClass + methodName +#p0")
    public Page<NewJob> queryByPageNoClose(int page) {
        int size=10;
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        Pageable pageable = new PageRequest(page, size, sort);
        return newJobDao.findAllByorUse(1,pageable);
    }

    @Override
    @Cacheable(key = "targetClass + methodName +#p0")
    public Page<NewJob> queryByPage(int page, int limit) {
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        Pageable pageable = new PageRequest(page, limit, sort);
        return newJobDao.findAll(pageable);
    }

    /**
     * 最热帖子
     */
    @Override
    @Cacheable(key = "targetClass + methodName +#p0")
    public Page<NewJob> queryHotByPage(int page) {
        int size=10;
        Sort sort = new Sort(Sort.Direction.DESC, "count");
        Pageable pageable = new PageRequest(page, size, sort);
        return newJobDao.findAllByorUse(1,pageable);
    }

    @Override
    @Cacheable(key = "targetClass + methodName +#p0")
    public Page<NewJob> queryByNameAndSortAndPage(String name,String jobClass, int page) {
        int size = 10;
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        Pageable pageable = new PageRequest(page, size, sort);
        return newJobDao.findAllByorUseAndJobClassAndJobNameLike(1,jobClass,"%"+name+"%",pageable);
    }

    @Override
    @Cacheable(key = "targetClass + methodName +#p0")
    public Page<NewJob> queryByNameAndPage(String name,int page) {
        int size = 10;
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        Pageable pageable = new PageRequest(page, size, sort);
        return newJobDao.findAllByorUseAndJobNameLike(1,"%"+name+"%",pageable);
    }

    @Override
    @Transactional
    public void updataCount(int id) {
        NewJob newJob = newJobDao.findAllById(id);
        newJob.setCount(newJob.getCount()+1);
    }

    @Override
    @Transactional
    public void updataOrUse(int id, int or) {
        NewJob newJob = newJobDao.findAllById(id);
        newJob.setOrUse(or);
    }

    @Override
    @Transactional
    public int updataOrUse(int id) {
        NewJob newJob = newJobDao.findAllById(id);
        if (newJob.getOrUse() == 0) {
            newJob.setOrUse(1);
            return 1;
        }else{
            newJob.setOrUse(0);
            return 0;
        }
    }

}
